package com.ymatou.iapi.exp.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.exp.parameter.ParamExt;
import com.ymatou.iapi.exp.parameter.SubscibeCallbackBean;
import com.ymatou.iapi.exp.service.SubscibeCallbackCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ExpCallService;
import com.ymttest.database.model.UserExp;
import com.ymttest.database.model.UserExpDetail;
import com.ymttest.database.sqlwapper.ExpWapper;
import com.ymttest.utils.TestDataManager;
/**
*交易服务事件回调
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SubscibeCallback{
	private static SubscibeCallbackBean subscibecallbackBean;
	private static SubscibeCallbackCall subscibecallbackCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("交易服务事件回调");
	}

	@Before
	public void caseUp() {
		subscibecallbackBean=new SubscibeCallbackBean();
		subscibecallbackCall=new SubscibeCallbackCall();
		ExpCallService.resetFestivalRateConfig(1, false);
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(subscibecallbackCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SubscibeCallback_001() {
		Logger.start(true,"交易服务事件回调成功,确认收货增加经验值");
		try {
			int uid=11;
			ExpWapper ew = new ExpWapper();
			ExpCallService.resetExp(uid, 0);
			subscibecallbackBean.setAppId("autoTest");
			subscibecallbackBean.setBusinessType(4);
			subscibecallbackBean.setMainOrderId(TestDataManager.getRandomNum(8));
			subscibecallbackBean.setBuyerId(uid);
			subscibecallbackBean.setSellerId(111);
			subscibecallbackBean.setOrderId(1+TestDataManager.getRandomNum(8));
			
			subscibecallbackBean.setYmtCouponCode(TestDataManager.getRandomUUID().toString());
			subscibecallbackBean.setSellerCouponCode(TestDataManager.getRandomUUID().toString());
			
			
			ParamExt ParamExt=new ParamExt();
			ParamExt.setBizCode("100102");
			ParamExt.setTotalPrice("120");
			ParamExt.setIsShippedByXlobo(false);
			
			subscibecallbackBean.setParamExt(ParamExt);
			subscibecallbackCall.setData(subscibecallbackBean);
			subscibecallbackCall.callService();			
			Logger.verifyEquals("ok", subscibecallbackCall.getReturnData(), "验证returndata");
			
			
			UserExp exp = ew.selectuserexp(uid).get(0);
			Logger.verifyEquals(ParamExt.getTotalPrice(), exp.getExp().toString(), "验证exp");
			UserExpDetail expd = ew.selectuserexpdetail(uid, null, null, null).get(0);
			Logger.verifyEquals(ParamExt.getTotalPrice(), expd.getExp().toString(), "验证expdetail");
			Logger.verifyEquals(subscibecallbackBean.getParamExt().getBizCode(), expd.getBizCode().toString(), "验证BizCode");
			Logger.verifyEquals(subscibecallbackBean.getOrderId(), expd.getBizNo(), "验证BizNo");
			Logger.verifyEquals(subscibecallbackBean.getOrderId(), expd.getOriginalNo(), "验证OriginalNo");
			Logger.verifyEquals(1, expd.getOperateType(), "验证OperateType1增加");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SubscibeCallback_002() {
		Logger.start(true,"交易服务事件回调成功,取消订单扣除经验值");
		try {
			int uid=12;
			ExpWapper ew = new ExpWapper();
			ExpCallService.resetExp(uid, 0);
			subscibecallbackBean.setAppId("autoTest");
			subscibecallbackBean.setBusinessType(4);
			subscibecallbackBean.setMainOrderId(TestDataManager.getRandomNum(8));
			subscibecallbackBean.setBuyerId(uid);
			subscibecallbackBean.setSellerId(111);
			subscibecallbackBean.setOrderId(1+TestDataManager.getRandomNum(8));
			
			subscibecallbackBean.setYmtCouponCode(TestDataManager.getRandomUUID().toString());
			subscibecallbackBean.setSellerCouponCode(TestDataManager.getRandomUUID().toString());
			
			
			ParamExt ParamExt=new ParamExt();
			ParamExt.setBizCode("100102");
			ParamExt.setTotalPrice("120");
			ParamExt.setIsShippedByXlobo(false);
			
			subscibecallbackBean.setParamExt(ParamExt);
			subscibecallbackCall.setData(subscibecallbackBean);
			subscibecallbackCall.callService();			
			Logger.verifyEquals("ok", subscibecallbackCall.getReturnData(), "验证returndata");
			
			Thread.sleep(1000);
			ParamExt.setBizCode("100202");
			subscibecallbackCall.setData(subscibecallbackBean);
			subscibecallbackCall.callService();			
			Logger.verifyEquals("ok", subscibecallbackCall.getReturnData(), "验证returndata");
			
			UserExp exp = ew.selectuserexp(uid).get(0);
			Logger.verifyEquals("0", exp.getExp().toString(), "验证exp");
			UserExpDetail expd = ew.selectuserexpdetail(uid, null, null, null).get(0);
			Logger.verifyEquals("-"+ParamExt.getTotalPrice(), expd.getExp().toString(), "验证expdetail");
			Logger.verifyEquals(subscibecallbackBean.getParamExt().getBizCode(), expd.getBizCode().toString(), "验证BizCode");
			Logger.verifyEquals(subscibecallbackBean.getOrderId(), expd.getBizNo(), "验证BizNo");
			Logger.verifyEquals(subscibecallbackBean.getOrderId(), expd.getOriginalNo(), "验证OriginalNo");
			Logger.verifyEquals(2, expd.getOperateType(), "验证OperateType2减少");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SubscibeCallback_003() {
		Logger.start(true,"交易服务事件回调成功,部分退款扣除经验值");
		try {
			int uid=13;
			ExpWapper ew = new ExpWapper();
			ExpCallService.resetExp(uid, 0);
			subscibecallbackBean.setAppId("autoTest");
			subscibecallbackBean.setBusinessType(4);
			subscibecallbackBean.setMainOrderId(TestDataManager.getRandomNum(8));
			subscibecallbackBean.setBuyerId(uid);
			subscibecallbackBean.setSellerId(111);
			subscibecallbackBean.setOrderId("9"+TestDataManager.getRandomNum(8));
			
			subscibecallbackBean.setYmtCouponCode(TestDataManager.getRandomUUID().toString());
			subscibecallbackBean.setSellerCouponCode(TestDataManager.getRandomUUID().toString());
			
			
			ParamExt ParamExt=new ParamExt();
			ParamExt.setBizCode("100102");
			ParamExt.setTotalPrice("120");
			ParamExt.setIsShippedByXlobo(false);
			
			subscibecallbackBean.setParamExt(ParamExt);
			subscibecallbackCall.setData(subscibecallbackBean);
			subscibecallbackCall.callService();			
			Logger.verifyEquals("ok", subscibecallbackCall.getReturnData(), "验证returndata");
			
			Thread.sleep(1000);
			ParamExt.setBizCode("100202");
			ParamExt.setPrice("30");
			ParamExt.setSpecId(TestDataManager.getRandomUUID().toString());
			subscibecallbackCall.setData(subscibecallbackBean);
			subscibecallbackCall.callService();			
			Logger.verifyEquals("ok", subscibecallbackCall.getReturnData(), "验证returndata");
			
			UserExp exp = ew.selectuserexp(uid).get(0);
			Logger.verifyEquals("90", exp.getExp().toString(), "验证exp");
			UserExpDetail expd = ew.selectuserexpdetail(uid, null, null, null).get(0);
			Logger.verifyEquals("-"+ParamExt.getPrice(), expd.getExp().toString(), "验证expdetail");
			Logger.verifyEquals(subscibecallbackBean.getParamExt().getBizCode(), expd.getBizCode().toString(), "验证BizCode");
			Logger.verifyEquals(subscibecallbackBean.getOrderId(), expd.getBizNo(), "验证BizNo");
			Logger.verifyEquals(subscibecallbackBean.getParamExt().getSpecId(), expd.getOriginalNo(), "验证OriginalNo");
			Logger.verifyEquals(2, expd.getOperateType(), "验证OperateType2减少");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SubscibeCallback_004() {
		Logger.start(true,"交易服务事件回调成功,多次部分退款扣除经验值");
		try {
			//120-30-40
			int uid=14;
			ExpWapper ew = new ExpWapper();
			ExpCallService.resetExp(uid, 0);
			subscibecallbackBean.setAppId("autoTest");
			subscibecallbackBean.setBusinessType(4);
			subscibecallbackBean.setMainOrderId(TestDataManager.getRandomNum(8));
			subscibecallbackBean.setBuyerId(uid);
			subscibecallbackBean.setSellerId(111);
			subscibecallbackBean.setOrderId(1+TestDataManager.getRandomNum(8));
			
			subscibecallbackBean.setYmtCouponCode(TestDataManager.getRandomUUID().toString());
			subscibecallbackBean.setSellerCouponCode(TestDataManager.getRandomUUID().toString());
			
			
			ParamExt ParamExt=new ParamExt();
			ParamExt.setBizCode("100102");
			ParamExt.setTotalPrice("120");
			ParamExt.setIsShippedByXlobo(false);
			
			subscibecallbackBean.setParamExt(ParamExt);
			subscibecallbackCall.setData(subscibecallbackBean);
			subscibecallbackCall.callService();			
			Logger.verifyEquals("ok", subscibecallbackCall.getReturnData(), "验证returndata");
			
			Thread.sleep(1000);
			ParamExt.setBizCode("100202");
			ParamExt.setPrice("30");
			ParamExt.setSpecId(TestDataManager.getRandomUUID().toString());
			subscibecallbackCall.setData(subscibecallbackBean);
			subscibecallbackCall.callService();			
			Logger.verifyEquals("ok", subscibecallbackCall.getReturnData(), "验证returndata");
			
			Thread.sleep(1000);
			ParamExt.setPrice("40");
			ParamExt.setSpecId(TestDataManager.getRandomUUID().toString());
			subscibecallbackCall.callService();			
			Logger.verifyEquals("ok", subscibecallbackCall.getReturnData(), "验证returndata");
			
			UserExp exp = ew.selectuserexp(uid).get(0);
			Logger.verifyEquals("50", exp.getExp().toString(), "验证exp");
			UserExpDetail expd = ew.selectuserexpdetail(uid, null, null, null).get(0);
			Logger.verifyEquals("-"+ParamExt.getPrice(), expd.getExp().toString(), "验证expdetail");
			Logger.verifyEquals(subscibecallbackBean.getParamExt().getBizCode(), expd.getBizCode().toString(), "验证BizCode");
			Logger.verifyEquals(subscibecallbackBean.getOrderId(), expd.getBizNo(), "验证BizNo");
			Logger.verifyEquals(subscibecallbackBean.getParamExt().getSpecId(), expd.getOriginalNo(), "验证OriginalNo");
			Logger.verifyEquals(2, expd.getOperateType(), "验证OperateType2减少");	
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SubscibeCallback_005() {
		Logger.start(false,"交易服务事件回调成功,多次部分退款扣除经验值最后一次金额超出");
		try {
			//120-30-40
			int uid=15;
			ExpWapper ew = new ExpWapper();
			ExpCallService.resetExp(uid, 0);
			subscibecallbackBean.setAppId("autoTest");
			subscibecallbackBean.setBusinessType(4);
			subscibecallbackBean.setMainOrderId(TestDataManager.getRandomNum(8));
			subscibecallbackBean.setBuyerId(uid);
			subscibecallbackBean.setSellerId(111);
			subscibecallbackBean.setOrderId(1+TestDataManager.getRandomNum(8));
			
			subscibecallbackBean.setYmtCouponCode(TestDataManager.getRandomUUID().toString());
			subscibecallbackBean.setSellerCouponCode(TestDataManager.getRandomUUID().toString());
			
			
			ParamExt ParamExt=new ParamExt();
			ParamExt.setBizCode("100102");
			ParamExt.setTotalPrice("120");
			ParamExt.setIsShippedByXlobo(false);
			
			subscibecallbackBean.setParamExt(ParamExt);
			subscibecallbackCall.setData(subscibecallbackBean);
			subscibecallbackCall.callService();			
			Logger.verifyEquals("ok", subscibecallbackCall.getReturnData(), "验证returndata");
			
			Thread.sleep(1000);
			ParamExt.setBizCode("100202");
			ParamExt.setPrice("100");
			ParamExt.setSpecId(TestDataManager.getRandomUUID().toString());
			subscibecallbackCall.setData(subscibecallbackBean);
			subscibecallbackCall.callService();			
			Logger.verifyEquals("ok", subscibecallbackCall.getReturnData(), "验证returndata");
			
			Thread.sleep(1000);
			ParamExt ParamExt2=new ParamExt();
			ParamExt2.setPrice("40");
			ParamExt2.setSpecId(TestDataManager.getRandomUUID().toString());
			ParamExt2.setBizCode("100202");
			subscibecallbackBean.setParamExt(ParamExt2);
			subscibecallbackCall.callService();			
			Logger.verifyEquals("fail", subscibecallbackCall.getReturnData(), "验证returndata");
			
			//验证第二次没有记录
			UserExp exp = ew.selectuserexp(uid).get(0);
			Logger.verifyEquals("20", exp.getExp().toString(), "验证exp");
			int size= ew.selectuserexpdetail(uid, null, null, ParamExt2.getSpecId()).size();
			Logger.verifyEquals(0, size, "验证size");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_SubscibeCallback_006() {
		Logger.start(true,"交易服务事件回调成功,部分退款扣除经验值后再调用全部退款");
		try {
			int uid=15;
			ExpWapper ew = new ExpWapper();
			ExpCallService.resetExp(uid, 0);
			subscibecallbackBean.setAppId("autoTest");
			subscibecallbackBean.setBusinessType(4);
			subscibecallbackBean.setMainOrderId(TestDataManager.getRandomNum(8));
			subscibecallbackBean.setBuyerId(uid);
			subscibecallbackBean.setSellerId(111);
			subscibecallbackBean.setOrderId("9"+TestDataManager.getRandomNum(8));
			
			subscibecallbackBean.setYmtCouponCode(TestDataManager.getRandomUUID().toString());
			subscibecallbackBean.setSellerCouponCode(TestDataManager.getRandomUUID().toString());
			
			
			ParamExt ParamExt=new ParamExt();
			ParamExt.setBizCode("100102");
			ParamExt.setTotalPrice("120");
			ParamExt.setIsShippedByXlobo(false);
			
			subscibecallbackBean.setParamExt(ParamExt);
			subscibecallbackCall.setData(subscibecallbackBean);
			subscibecallbackCall.callService();			
			Logger.verifyEquals("ok", subscibecallbackCall.getReturnData(), "验证returndata");
			
			Thread.sleep(1000);
			ParamExt.setBizCode("100202");
			ParamExt.setPrice("30");
			ParamExt.setSpecId(TestDataManager.getRandomUUID().toString());
			subscibecallbackCall.setData(subscibecallbackBean);
			subscibecallbackCall.callService();			
			Logger.verifyEquals("ok", subscibecallbackCall.getReturnData(), "验证returndata");
			ParamExt.setSpecId(TestDataManager.getRandomUUID().toString());
			subscibecallbackCall.callService();			
			Logger.verifyEquals("ok", subscibecallbackCall.getReturnData(), "验证returndata");
			
			Thread.sleep(1000);
			ParamExt ParamExt2=new ParamExt();
			ParamExt2.setBizCode("100202");
			subscibecallbackBean.setParamExt(ParamExt2);
			subscibecallbackCall.setData(subscibecallbackBean);
			subscibecallbackCall.callService();			
			Logger.verifyEquals("ok", subscibecallbackCall.getReturnData(), "验证returndata");
			
			UserExp exp = ew.selectuserexp(uid).get(0);
			Logger.verifyEquals("0", exp.getExp().toString(), "验证exp");
			UserExpDetail expd = ew.selectuserexpdetail(uid, null, null, null).get(0);
			//剩余的金额全扣 120-30-30
			Logger.verifyEquals("-60", expd.getExp().toString(), "验证expdetail");
			Logger.verifyEquals(ParamExt.getBizCode(), expd.getBizCode().toString(), "验证BizCode");
			Logger.verifyEquals(subscibecallbackBean.getOrderId(), expd.getBizNo(), "验证BizNo");
			Logger.verifyEquals(subscibecallbackBean.getOrderId(), expd.getOriginalNo(), "验证OriginalNo");
			Logger.verifyEquals(2, expd.getOperateType(), "验证OperateType2减少");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SubscibeCallback_007() {
		Logger.start(true,"交易服务事件回调重复调用,确认收货增加经验值");
		try {
			//如果插入了重复数据类的异常返回“ok”给总线
			int uid=11;
			ExpWapper ew = new ExpWapper();
			ExpCallService.resetExp(uid, 0);
			subscibecallbackBean.setAppId("autoTest");
			subscibecallbackBean.setBusinessType(4);
			subscibecallbackBean.setMainOrderId(TestDataManager.getRandomNum(8));
			subscibecallbackBean.setBuyerId(uid);
			subscibecallbackBean.setSellerId(111);
			subscibecallbackBean.setOrderId(1+TestDataManager.getRandomNum(8));
			
			subscibecallbackBean.setYmtCouponCode(TestDataManager.getRandomUUID().toString());
			subscibecallbackBean.setSellerCouponCode(TestDataManager.getRandomUUID().toString());
			
			
			ParamExt ParamExt=new ParamExt();
			ParamExt.setBizCode("100102");
			ParamExt.setTotalPrice("120");
			ParamExt.setIsShippedByXlobo(false);
			
			subscibecallbackBean.setParamExt(ParamExt);
			subscibecallbackCall.setData(subscibecallbackBean);
			subscibecallbackCall.callService();	
			Thread.sleep(1000);
			subscibecallbackCall.callService();			
			Logger.verifyEquals("ok", subscibecallbackCall.getReturnData(), "验证returndata");
			
			
			UserExp exp = ew.selectuserexp(uid).get(0);
			Logger.verifyEquals(ParamExt.getTotalPrice(), exp.getExp().toString(), "验证exp");
			UserExpDetail expd = ew.selectuserexpdetail(uid, null, null, null).get(0);
			Logger.verifyEquals(ParamExt.getTotalPrice(), expd.getExp().toString(), "验证expdetail");
			Logger.verifyEquals(subscibecallbackBean.getParamExt().getBizCode(), expd.getBizCode().toString(), "验证BizCode");
			Logger.verifyEquals(subscibecallbackBean.getOrderId(), expd.getBizNo(), "验证BizNo");
			Logger.verifyEquals(subscibecallbackBean.getOrderId(), expd.getOriginalNo(), "验证OriginalNo");
			Logger.verifyEquals(1, expd.getOperateType(), "验证OperateType1增加");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}